package com.example.demo.contorller;

import com.example.demo.bean.Profession;
import com.example.demo.bean.Regional;
import com.example.demo.common.ResponseCode;
import com.example.demo.common.ServerResponse;
import com.example.demo.dao.ProfessionRepository;
import com.example.demo.service.ProfessionService;
import com.example.demo.service.RegionalService;
import lombok.AllArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author ouyangzhixiang
 * @date 2018/6/23
 */
@RequestMapping("/profession/")
@RestController
@AllArgsConstructor
public class ProfessionController {

    private final ProfessionService professionService;
    @GetMapping(value = "profession_list")
    public ServerResponse<List<Profession>> professionList(Integer parentId) {
        return professionService.list(parentId);
    }

    @GetMapping(value = "select_find_one/{id}")

    public ServerResponse selectOne(@PathVariable("id") Integer id){
        return professionService.findOne(id);
    }

    @PostMapping("add_profession")
    public ServerResponse<Profession> addProfession(@Valid Profession profession, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        return professionService.addProfession(profession);
    }

    @DeleteMapping("delete_profession/{id}")
    public void deleteProfession(@PathVariable("id") Integer id) {
        professionService.deleteProfession(id);
    }

    @PutMapping("profession_update")
    public ServerResponse<Profession> professionUpdate(@Valid Profession profession,BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        ServerResponse<Profession> responseProfession = professionService.professionUpdate(profession);
        return responseProfession;
    }
}
